﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace TaskManager.Data
{
    public class SprintInMemoryRepository : ISprintRepository
    {
        private static IList<Developer> _developers;
        private static IList<Sprint> _sprints;
        private static IList<Task> _tasks;

        public IList<Developer> GetDevelopers()
        {
            if (_developers == null)
            {
                _developers = new List<Developer>();
                _developers.Add(new Developer { DeveloperId = 1, FirstName = "Developer", LastName = "One", Title = "Lead" });
                _developers.Add(new Developer { DeveloperId = 2, FirstName = "Developer", LastName = "Two", Title = "Sr. Dev." });
                _developers.Add(new Developer { DeveloperId = 3, FirstName = "Developer", LastName = "Three", Title = "Mid. Dev." });
            }
            return _developers;
        }

        public void SaveDeveloper(Developer developer, bool delete = false)
        {
            if (developer.DeveloperId == 0)
            {
                int id = _developers.Max(s => s.DeveloperId);
                id += 1;
                developer.DeveloperId = id;
                _developers.Add(developer);
            }
            else
            {
                Developer persistDeveloper = _developers.Where(m => m.DeveloperId == developer.DeveloperId).FirstOrDefault();

                if (delete)
                {
                    _developers.Remove(persistDeveloper);
                }
                else
                {
                    persistDeveloper.Copy(developer);
                }
            }
        }

        public IList<Sprint> GetSprints()
        {
            if (_sprints == null)
            {
                _sprints = new List<Sprint>();
                _sprints.Add(new Sprint { SprintId = 1, StartDate = new DateTime(2011, 10, 3), EndDate = new DateTime(2011, 11, 11) });
                _sprints.Add(new Sprint { SprintId = 2, StartDate = new DateTime(2011, 11, 14), EndDate = new DateTime(2011, 12, 23) });
                _sprints.Add(new Sprint { SprintId = 3, StartDate = new DateTime(2011, 1, 2), EndDate = new DateTime(2011, 2, 10) });
                _sprints.Add(new Sprint { SprintId = 4, StartDate = new DateTime(2011, 2, 13), EndDate = new DateTime(2011, 3, 23) });
            }
            return _sprints;
        }

        public string SaveSprint(Sprint sprint, bool delete = false)
        {
            string message = string.Empty;

            if (sprint.SprintId == 0)
            {
                int id = _sprints.Max(s => s.SprintId);
                id += 1;
                sprint.SprintId = id;
                _sprints.Add(sprint);
            }
            else
            {
                Sprint persistSprint = _sprints.Where(m => m.SprintId == sprint.SprintId).FirstOrDefault();

                if (delete)
                {
                    _sprints.Remove(persistSprint);
                }
                else
                {
                    // Does the sprint overlap with any other sprints?
                    Sprint overlappingSprint = _sprints.Where(m => m.SprintId != sprint.SprintId && m.StartDate <= sprint.EndDate && m.EndDate >= sprint.StartDate).FirstOrDefault();

                    // If not...
                    if (overlappingSprint == null)
                    {
                        persistSprint.Copy(sprint);
                    }
                    else
                    {
                        message = string.Format("The sprint start and end dates overlap with sprint {0} that has a start date of {1} and an end date of {2}.  Sprint dates cannot overlap.",
                            overlappingSprint.SprintId, overlappingSprint.StartDate.ToShortDateString(), overlappingSprint.EndDate.ToShortDateString());
                    }
                }
            }

            return message;
        }

        /*
        private bool DoesOverlap(DateTime startDate1, DateTime endDate1, DateTime startDate2, DateTime endDate2)
        {
            if (startDate2 <= endDate1 && endDate2 >= startDate1)
            {
                return true;
            }
            return false;
        }
         */



        public IList<Task> GetTasks()
        {
            if (_tasks == null)
            {
                _tasks = new List<Task>();

                // Sprint 1
                _tasks.Add(new Task { TaskId = 1, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Web Application", SprintId = 1, AssignedToId = 1, EstimatedHours = 1, CompletedDate = new DateTime(2011, 10, 10) });
                _tasks.Add(new Task { TaskId = 2, TypeOfTask = (int)TaskType.Scenerio, Description = "Configure Application to Use Kendo UI", SprintId = 1, AssignedToId = 1, EstimatedHours = 1, CompletedDate = new DateTime(2011, 10, 15) });
                _tasks.Add(new Task { TaskId = 3, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Models", SprintId = 1, AssignedToId = 2, EstimatedHours = 1, CompletedDate = new DateTime(2011, 10, 20) });
                _tasks.Add(new Task { TaskId = 4, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Sprint Repository", SprintId = 1, AssignedToId = 2, EstimatedHours = 2, CompletedDate = new DateTime(2011, 10, 25) });
                _tasks.Add(new Task { TaskId = 5, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Home Controller", SprintId = 1, AssignedToId = 2, EstimatedHours = 1, CompletedDate = new DateTime(2011, 10, 30) });
                _tasks.Add(new Task { TaskId = 6, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Index View", SprintId = 1, AssignedToId = 3, EstimatedHours = 1, CompletedDate = new DateTime(2011, 11, 5) });
                _tasks.Add(new Task { TaskId = 7, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Navigation Partial View", SprintId = 1, AssignedToId = 3, EstimatedHours = 1, CompletedDate = new DateTime(2011, 11, 10) });

                // Sprint 2
                _tasks.Add(new Task { TaskId = 8, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Sprint Partial View", SprintId = 2, AssignedToId = 1, EstimatedHours = 1, CompletedDate = new DateTime(2011, 11, 20) });
                _tasks.Add(new Task { TaskId = 9, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Sprint Edit Form", SprintId = 2, AssignedToId = 1, EstimatedHours = 2, CompletedDate = new DateTime(2011, 11, 25) });
                _tasks.Add(new Task { TaskId = 10, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Sprint Task Grid", SprintId = 2, AssignedToId = 2, EstimatedHours = 1, CompletedDate = new DateTime(2011, 11, 30) });
                _tasks.Add(new Task { TaskId = 11, TypeOfTask = (int)TaskType.Scenerio, Description = "Provide Edit Mode for Sprint Task Grid", SprintId = 2, AssignedToId = 2, EstimatedHours = 1, CompletedDate = new DateTime(2011, 12, 7) });
                _tasks.Add(new Task { TaskId = 12, TypeOfTask = (int)TaskType.Scenerio, Description = "Create JSON Parameter Attribute", SprintId = 2, AssignedToId = 3, EstimatedHours = 1, CompletedDate = new DateTime(2011, 12, 14) });
                _tasks.Add(new Task { TaskId = 13, TypeOfTask = (int)TaskType.Scenerio, Description = "Create Update Controller Actions for Sprint", SprintId = 2, AssignedToId = 3, EstimatedHours = 4, CompletedDate = new DateTime(2011, 12, 21) });
            }
            return _tasks;
        }

        public IList<Task> GetTasks(Sprint sprint)
        {
            return this.GetTasks().Where(t => t.SprintId == sprint.SprintId).ToList();
        }

        public IList<Task> GetTasks(Developer developer)
        {
            return this.GetTasks().Where(t => t.AssignedToId == developer.DeveloperId).ToList();
        }

        public void SaveTask(Task task, bool delete)
        {
            if (task.TaskId == 0)
            {
                int id = _tasks.Max(s => s.TaskId);
                id += 1;
                task.TaskId = id;
                _tasks.Add(task);
            }
            else
            {
                Task persistTask = _tasks.Where(m => m.TaskId == task.TaskId).FirstOrDefault();

                if (delete)
                {
                    _tasks.Remove(persistTask);
                }
                else
                {
                    persistTask.Copy(task);
                }
            }
        }
    }
}